package com.qms.qep.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.qms.common.utils.DateUtils;
import com.qms.common.utils.StringUtils;
import com.qms.common.utils.file.FileUploadUtils;
import com.qms.common.utils.file.MimeTypeUtils;
import com.qms.qep.domain.InspectionAttachment;
import com.qms.qep.mapper.InspectionAttachmentMapper;
import com.qms.qep.service.IInspectionAttachmentService;

/**
 * 检验附件Service业务层处理
 * 
 * @author qms
 * @date 2025-05-14
 */
@Service
public class InspectionAttachmentServiceImpl implements IInspectionAttachmentService 
{
    @Autowired
    private InspectionAttachmentMapper inspectionAttachmentMapper;

    @Value("${qms.profile}")
    private String uploadPath;

    /**
     * 查询检验附件
     * 
     * @param id 检验附件主键
     * @return 检验附件
     */
    @Override
    public InspectionAttachment selectInspectionAttachmentById(Long id)
    {
        return inspectionAttachmentMapper.selectInspectionAttachmentById(id);
    }

    /**
     * 根据任务ID查询检验附件列表
     * 
     * @param inspectionTaskId 检验任务ID
     * @return 检验附件集合
     */
    @Override
    public List<InspectionAttachment> selectInspectionAttachmentByTaskId(Long inspectionTaskId)
    {
        return inspectionAttachmentMapper.selectInspectionAttachmentByTaskId(inspectionTaskId);
    }

    /**
     * 查询检验附件列表
     * 
     * @param inspectionAttachment 检验附件
     * @return 检验附件
     */
    @Override
    public List<InspectionAttachment> selectInspectionAttachmentList(InspectionAttachment inspectionAttachment)
    {
        return inspectionAttachmentMapper.selectInspectionAttachmentList(inspectionAttachment);
    }

    /**
     * 新增检验附件
     * 
     * @param inspectionAttachment 检验附件
     * @return 结果
     */
    @Override
    public int insertInspectionAttachment(InspectionAttachment inspectionAttachment)
    {
        inspectionAttachment.setCreateTime(DateUtils.getNowDate());
        return inspectionAttachmentMapper.insertInspectionAttachment(inspectionAttachment);
    }

    /**
     * 修改检验附件
     * 
     * @param inspectionAttachment 检验附件
     * @return 结果
     */
    @Override
    public int updateInspectionAttachment(InspectionAttachment inspectionAttachment)
    {
        return inspectionAttachmentMapper.updateInspectionAttachment(inspectionAttachment);
    }

    /**
     * 批量删除检验附件
     * 
     * @param ids 需要删除的检验附件主键
     * @return 结果
     */
    @Override
    public int deleteInspectionAttachmentByIds(Long[] ids)
    {
        return inspectionAttachmentMapper.deleteInspectionAttachmentByIds(ids);
    }

    /**
     * 删除检验附件信息
     * 
     * @param id 检验附件主键
     * @return 结果
     */
    @Override
    public int deleteInspectionAttachmentById(Long id)
    {
        return inspectionAttachmentMapper.deleteInspectionAttachmentById(id);
    }
    
    /**
     * 上传文件
     * 
     * @param file 文件
     * @param taskId 任务ID
     * @param remark 备注
     * @return 检验附件
     * @throws Exception 上传异常
     */
    @Override
    public InspectionAttachment uploadFile(MultipartFile file, Long taskId, String remark) throws Exception
    {
        if (file == null || taskId == null) {
            throw new IOException("上传文件或任务ID不能为空");
        }
        
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        if (StringUtils.isEmpty(originalFilename)) {
            originalFilename = "file_" + System.currentTimeMillis();
        }
        
        // 获取文件扩展名
        String extension = FilenameUtils.getExtension(originalFilename);
        
        // 上传文件路径
        String filePath = FileUploadUtils.upload(uploadPath + "/inspection", file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        
        InspectionAttachment attachment = new InspectionAttachment();
        attachment.setInspectionTaskId(taskId);
        attachment.setFileName(originalFilename);
        attachment.setFileType(extension);
        attachment.setFilePath(filePath);
        attachment.setUploadDate(DateUtils.getNowDate());
        attachment.setCreateTime(DateUtils.getNowDate());
        attachment.setRemark(remark);
        
        insertInspectionAttachment(attachment);
        
        return attachment;
    }
} 